package oneD.fem.visualize;

import oneD.dynamics.DynamicStructure;
import inf.v3d.obj.CylinderSet;
import inf.v3d.view.Viewer;

public class DynamicTrussVisualizer extends TrussVisualizer {

	private DynamicStructure dStruct_;
	private int step_;

	public DynamicTrussVisualizer(DynamicStructure dStruct, Viewer view) {
		super(dStruct.getStaticStructure(), view);
		this.dStruct_ = dStruct;
		this.step_ = 0;
	}

	@Override
	public void drawDisplacedStructure() {
		CylinderSet c = new CylinderSet();
		c.setColor("cyan");
		for (int i = 0; i < this.struct_.getNumberOfElements(); i++) {
			int id0 = this.struct_.getElement(i).getNode(0).getId();
			int id1 = this.struct_.getElement(i).getNode(1).getId();
			c.addCylinder((this.struct_.getElement(i).getNode(0).getPosition()
					.add(this.dStruct_.getNode(id0).getDisplacement(step_)
							.multiply(this.displacementScale_))).toArray(),
					(this.struct_.getElement(i).getNode(1).getPosition()
							.add(this.dStruct_.getNode(id1)
									.getDisplacement(step_)
									.multiply(this.displacementScale_)))
							.toArray(), this.areaScale_
							* this.struct_.getElement(i).getArea());
		}
		this.view_.addObject3D(c);
	}

	@Override
	public boolean advance() {
		if (step_ < dStruct_.getNumberOfStep() - 1)
			step_++;
		else {
			step_ = dStruct_.getNumberOfStep() - 1;
			return true;
		}
		return false;
	}

	@Override
	public boolean retreat() {
		if (step_ > 1)
			step_--;
		else {
			step_ = 0;
			return true;
		}
		return false;
	}
}
